<?php
namespace plugin\admin\app\middleware;

use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
use support\Log;

class RequestLog implements MiddlewareInterface
{
    public function process(Request $request, callable $handler): Response
    {
        $start_time = microtime(true);

        $response = $handler($request);

        $end_time = microtime(true);

        $cost_time = ($end_time - $start_time) * 1000;

        Log::channel('plugin.admin.infoLog')->info('admin插件请求日志', [
            'path'=>$request->uri(),
            'method'=>$request->method(),
            'user'=>admin('nickname'),
            'ip'=>$request->getRealIp(),
            'response'=>$response->rawBody(),
            'cost_time'=>$cost_time
        ]);

        return $response;
    }

}